package com.dl7.player.utils;

import ohos.agp.utils.Point;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Created by Rukey7 on 2016/11/21.
 */

public final class MotionEventUtils {

    public static final int FINGER_FLAG_1 = 601;
    public static final int FINGER_FLAG_2 = 602;
    public static final int FINGER_FLAG_3 = 603;

    private MotionEventUtils() {
        throw new AssertionError();
    }


    /**
     * Determine the space between the first two fingers
     */
    public static float calcSpacing(TouchEvent event, int index1, int index2) {
        float x = event.getPointerPosition(index1).getX() - event.getPointerPosition(index2).getX();
        float y = event.getPointerPosition(index1).getY() - event.getPointerPosition(index2).getY();
        return (float) Math.sqrt(x * x + y * y);
    }

    /**
     * Determine the space between the two fingers
     * @param event
     * @param fingerFlag
     * @return
     */
    public static float calcSpacing(TouchEvent event, int fingerFlag) {
        float x, y;
        if (FINGER_FLAG_1 == fingerFlag) {
            x = (event.getPointerPosition(0).getX() + event.getPointerPosition(1).getX()) / 2
                    - event.getPointerPosition(2).getX();
            y = (event.getPointerPosition(0).getY() + event.getPointerPosition(1).getY()) / 2
                    - event.getPointerPosition(2).getY();
        } else if (FINGER_FLAG_2 == fingerFlag) {
            x = (event.getPointerPosition(0).getX() + event.getPointerPosition(2).getX()) / 2
                    - event.getPointerPosition(1).getX();
            y = (event.getPointerPosition(0).getY() + event.getPointerPosition(2).getY()) / 2
                    - event.getPointerPosition(1).getY();
        } else if (FINGER_FLAG_3 == fingerFlag) {
            x = (event.getPointerPosition(2).getX() + event.getPointerPosition(1).getX()) / 2
                    - event.getPointerPosition(0).getX();
            y = (event.getPointerPosition(2).getY() + event.getPointerPosition(1).getY()) / 2
                    - event.getPointerPosition(0).getY();
        } else {
            x = (event.getPointerPosition(0).getX() - event.getPointerPosition(1).getX());
            y = (event.getPointerPosition(0).getY() - event.getPointerPosition(1).getY());
        }
        return (float) Math.sqrt(x * x + y * y);
    }

    /**
     * Calculate the mid point of two pointers
     */
    public static void midPoint(Point point, TouchEvent event) {
        float x = event.getPointerPosition(0).getX() + event.getPointerPosition(1).getX()
                + event.getPointerPosition(2).getX();
        float y = event.getPointerPosition(0).getY() + event.getPointerPosition(1).getY()
                + event.getPointerPosition(2).getY();
        point.modify(x / 3, y / 3);
    }

    /**
     * 计算靠的最近的两指
     * @param event
     * @return
     */
    public static int calcFingerFlag(TouchEvent event) {
        float space1 = calcSpacing(event, 0, 1);
        float space2 = calcSpacing(event, 0, 2);
        float space3 = calcSpacing(event, 2, 1);
        float minSpace = Math.min(space1, Math.min(space2, space3));
        if (minSpace == space1) {
            return FINGER_FLAG_1;
        } else if (minSpace == space2) {
            return FINGER_FLAG_2;
        } else if (minSpace == space3) {
            return FINGER_FLAG_3;
        } else {
            return -1;
        }
    }

    /**
     * Calculate the degree to be rotated by.
     *
     * @param event
     * @return Degrees
     */
    public static float rotation(TouchEvent event, int fingerFlag) {
        double delta_x, delta_y;
        if (FINGER_FLAG_1 == fingerFlag) {
            delta_x = (event.getPointerPosition(0).getX() + event.getPointerPosition(1).getX()) / 2
                    - event.getPointerPosition(2).getX();
            delta_y = (event.getPointerPosition(0).getY() + event.getPointerPosition(1).getY()) / 2
                    - event.getPointerPosition(2).getY();
        } else if (FINGER_FLAG_2 == fingerFlag) {
            delta_x = (event.getPointerPosition(0).getX() + event.getPointerPosition(2).getX()) / 2
                    - event.getPointerPosition(1).getX();
            delta_y = (event.getPointerPosition(0).getY() + event.getPointerPosition(2).getY()) / 2
                    - event.getPointerPosition(1).getY();
        } else if (FINGER_FLAG_3 == fingerFlag) {
            delta_x = (event.getPointerPosition(2).getX() + event.getPointerPosition(1).getX()) / 2
                    - event.getPointerPosition(0).getX();
            delta_y = (event.getPointerPosition(2).getY() + event.getPointerPosition(1).getY()) / 2
                    - event.getPointerPosition(0).getY();
        } else {
            delta_x = (event.getPointerPosition(0).getX() - event.getPointerPosition(1).getX());
            delta_y = (event.getPointerPosition(0).getY() - event.getPointerPosition(1).getY());
        }
        double radians = Math.atan2(delta_y, delta_x);
        return (float) Math.toDegrees(radians);
    }
}
